﻿@model GroupShop.Model.PagerModel   
@using GroupShop.Utility;
<div class="pager">
    <ul>
        @{
            int i = 0;

            int pagerLength = 7;

            int midLength = (pagerLength / 2) + 1;

            if (Model.Index > 1)
            {

                if (Model.Index > 4 && Model.Total > pagerLength)
                {

            @Html.PagerLink(string.Empty, Url.Content(@"~\Product\Index\page1"), "首页");

                }
                
            @Html.PagerLink("previous", Url.Content(@"~\Product\Index\page" + (Model.Index - 1).ToString()), "上一页");

            }


            do
            {

                i++;

                if (Model.Index <= midLength)
                {                       

            @Html.PagerLink((i == Model.Index ? "selected" : string.Empty), Url.Content(@"~\Product\Index\page" + i.ToString()), i.ToString());

                }
                else if (Model.Index > midLength && Model.Total - Model.Index >= midLength - 1)
                {
                    var index = Model.Index - midLength + i;
            @Html.PagerLink((i == midLength ? "selected" : string.Empty), Url.Content(@"~\Product\Index\page" + index.ToString()), index.ToString());

                }
                else if (Model.Total >= pagerLength)
                {
                    var index = Model.Total - pagerLength + i;
            @Html.PagerLink((Model.Index == index ? "selected" : string.Empty), Url.Content(@"~\Product\Index\page" + index.ToString()), index.ToString());

                }

                else
                {
            @Html.PagerLink((Model.Index == i ? "selected" : string.Empty), Url.Content(@"~\Product\Index\page" + i.ToString()), i.ToString());

                }

            } while (i < pagerLength && i < Model.Total);

            if (Model.Total >= (Model.Index + 1))
            {
            @Html.PagerLink("next",
                Url.Content(@"~\Product\Index\page" + (Model.Index + 1).ToString()),
                "下一页");

                      if ((Model.Total - Model.Index) >= midLength)
                      {            
            @Html.PagerLink(string.Empty,
                        Url.Content(@"~\Product\Index\page" + Model.Total.ToString()),
                        "末页");
                      }

            }

           

        }
    </ul>
</div>
